Imports System.Runtime.Serialization

Namespace DataStream
    <Serializable(), ObfuscationAttribute(Exclude:=True, ApplyToMembers:=True)> _
    Public Class StreamLogDBItem
        Private mStartTime As DateTime
        Private mLogbytes As Byte()
        Private mLength As UInt32 = 0

        <OptionalField(VersionAdded:=2)> _
        Private mLogName As String = ""

        ''' <summary>
        ''' Creates a new log DB Item
        ''' </summary>
        ''' <param name="log"></param>
        ''' <remarks></remarks>
        Sub New(ByVal log As StreamLog)
            Me.Log = log
            mStartTime = log.StartTime
            mLogName = log.LogName
        End Sub

        ''' <summary>
        ''' Gets the embedded stream log
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Log() As StreamLog
            Get
                Dim loader As New Modules.ObjectPersistance(Of StreamLog)
                Dim stream As StreamLog = Nothing

                ' uncompress and deserialise
                loader.LoadFromStreamAndUnCompress(New IO.MemoryStream(mLogbytes), stream)

                If Not IsNothing(stream) Then
                    ' set the starttime
                    stream.StartTime = mStartTime
                    stream.LogName = mLogName
                End If

                ' return proper formed object
                Return stream
            End Get
            Set(ByVal value As StreamLog)
                ' Me.mStartTime = value.StartTime
                Me.mLength = value.Count
                Me.mLogName = value.LogName
                Dim loader As New Modules.ObjectPersistance(Of StreamLog)
                Dim stream As IO.Stream = Nothing

                ' Compress and serialise
                loader.SaveToStreamAndCompress(stream, value)

                ' Save the bytes
                ReDim mLogbytes(stream.Length - 1)
                stream.Read(mLogbytes, 0, stream.Length)
                stream.Close()
            End Set
        End Property

        ''' <summary>
        ''' Gets the starttime for the log
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property StartTime() As DateTime
            Get
                Return mStartTime
            End Get
        End Property

        ''' <summary>
        ''' Gets the length/count of the log
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Length() As UInt32
            Get
                Return mLength
            End Get
        End Property

        ''' <summary>
        ''' Gets the datalog's Name
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property LogName() As String
            Get
                Return mLogName
            End Get
            Set(ByVal value As String)
                mLogName = value
            End Set
        End Property

    End Class

End Namespace

